<?php require_once('../../Connections/cotiza.php'); ?>
<?php
session_start();

$acc  = $_GET['acc'];
$vend = $_SESSION['id_qad'];
$permisos = $_SESSION['permisos'];

mysql_select_db($database_cotiza, $cotiza);
		
if($acc == "search")
{
	if($permisos == 0 || $permisos == 1)
		$condicion = " AND cm_vendedor = '$vend'";
	
	$b = $_POST['busqueda'];
	
	$query = "SELECT * FROM cm_mstr WHERE (cm_rut like '%$b%' or cm_sort like '%$b%' or cm_ciudad like '%$b%' or cm_contacto like '%$b%')".$condicion." ORDER BY cm_sort";
}
else
{
	if($permisos == 0 || $permisos == 1)
		$condicion = " WHERE cm_vendedor = '$vend'";
		
	$query = "SELECT * FROM cm_mstr".$condicion." ORDER BY cm_sort";
}
$resultados = mysql_query($query, $cotiza) or die(mysql_error());
$row = mysql_fetch_assoc($resultados);
$totalRows = mysql_num_rows($resultados);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>..:: Clientes ::..</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.default_title {
	color: #FFF;
}
-->
</style>
</head>

<body onload="document.form1.busqueda.focus();">
<form id="form1" name="form1" method="post" action="clientes.php?acc=search">
  <table border="0" align="center">
    <tr>
      <td >B&uacute;squeda de Cliente:</td>
      <td ><input name="busqueda" class="default" size="20" autocomplete="off" value="<?php echo $b;?>"/></td>
      <td ><input type="submit" value="Buscar" class="v8" /></td>
    </tr>
  </table>
</form>
<br />
<?php if($totalRows >0){?>
  <table border="1" align="center" cellpadding="2" cellspacing="0">
    <tr class="default_title">
      <td width="70" align="center" bgcolor="#0000CC">Rut</td>
      <td width="240" align="center" bgcolor="#0000CC">Ras&oacute;n social</td>
      <td width="80" align="center" bgcolor="#0000CC">Ciudad</td>
      <td width="170" align="center" bgcolor="#0000CC">Contacto</td>
    </tr>
    <?php do { ?>
    <tr>
      <td>
    <a href="javascript:window.opener.document.getElementById('rut_cte').value= '<?php echo str_replace(".","",$row['cm_rut']); ?>';             window.opener.document.getElementById('r_social').value = '<?php echo $row['cm_sort']; ?>'; 
    		 window.opener.document.getElementById('g_comercial').value = '<?php echo "GIRO" ?>';
             window.opener.document.getElementById('d_comercial').value = '<?php echo $row['cm_direccion']; ?>'; 
             window.opener.document.getElementById('ciudad').value = '<?php echo $row['cm_ciudad']; ?>';
             window.opener.document.getElementById('region').value = '<?php echo $row['cm_region']; ?>';
             window.opener.document.getElementById('rut_cte').focus();
             window.close();"> <?php echo str_replace(".","",$row['cm_rut']); ?></a>&nbsp;</td>
      <td><?php $n_cliente = $row['cm_sort'].$row['ad_line1']; echo substr($n_cliente,0,35); ?>&nbsp;</td>
      <td><?php echo $row['cm_ciudad']; ?>&nbsp;</td>
      <td><?php echo $row['cm_contacto']; ?>&nbsp;</td>
    </tr>
      <?php } while ($row = mysql_fetch_assoc($resultados)); ?>
  </table>
<?php }?>
</body>
</html>
<?php 
mysql_close($cotiza);
?>